home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
machack
/
Hacks97
/
NewsTicker.sit
/
NewsTicker
/
source code
/
Extractors
/
AppleExtractor.cp
next >
Wrap
Text File
|
1997-06-26
|
5KB
|
201 lines
/*------------------------------------------------------------------------------
#
# NewsTicker, my Hack for 1997
#
# AppleExtractor.cp - Derived from HTMLExtractor, we get passed the tokens
# and try to recognize headlines out of it. We're
# parsing "www.apple.com", Apple Computer's home page
#
------------------------------------------------------------------------------*/
#include "TickerGlobals.h"
#include <string.h>
#include "AppleExtractor.h"
#include "TickerCheckWebs.h"
#include "HTMLExtractor.h"
// Check every hour
#define kAppleNewsPeriod 3600
// Globals for the Apple Extractor
unsigned long gAppleNextTime = 0;
Str31 gAppleLastMod = "\p";
#define kAppleAddress "www.apple.com"
class AppleExtractor: public HTMLExtractor
{
protected:
enum AppleParser { knaParsing,
//stories are <strong>head</strong><br>stuff</p>
knaHasStrong, knaHasNotStrong, knaHasBreak,
//images are <TD> <a><img></a>
knaHasLink
};
AppleParser mfCurrentState;
Str255 mfTheURL;
Str255 mfTheSubject;
Boolean mfInTD;
public:
AppleExtractor(sMyDataPtr theDataPtr);
virtual ~AppleExtractor (void);
virtual void HandleToken(char* string, short numchars, Boolean isCommand);
};
//
// We just parse the entries to find the element
//
AppleExtractor::AppleExtractor(sMyDataPtr theDataPtr)
:HTMLExtractor(kAppleAddress, 1001, theDataPtr)
{
unsigned long now;
mfCurrentState = knaParsing; //just waiting for our thing to come through
mfInTD = false;
GetDateTime(&now);
gAppleNextTime = now + kAppleNewsPeriod; //refresh apple every hour
}
AppleExtractor::~AppleExtractor( void ) //remember the modification date
{
PLstrcpy(gAppleLastMod, mfLastModified);
}
void AppleExtractor::HandleToken(char* string, short numchars, Boolean isCommand)
{
Str255 thestr;
if (isCommand)
{
if (MyCompareStr(string, "<TD ")) //table delimiters mark the image links
mfInTD = true;
else if (MyCompareStr(string, "</TD "))
{
mfInTD = false;
mfCurrentState = knaParsing;
}
else switch (mfCurrentState)
{
case knaParsing: //from paragraph, we want <STRONG>
if (MyCompareStr(string, "<STRONG>"))
{
mfCurrentState = knaHasStrong;
mfTheSubject[0] = 0;
mfTheURL[0] = 0;
}
else if (MyCompareStr(string, "<A ")&&mfInTD)
{
if (HTMLExtractor::ParseGoodURL(string+2, mfTheURL))
{
mfCurrentState = knaHasLink;
mfTheSubject[0] = 0;
}
else mfCurrentState = knaParsing;
}
else mfCurrentState = knaParsing;
break;
case knaHasStrong: //for this, we only want </STRONG>
if (MyCompareStr(string, "</STRONG>")&&(mfTheSubject[0]>0))
mfCurrentState = knaHasNotStrong;
else mfCurrentState = knaParsing;
break;
case knaHasNotStrong: //from here, we need a <BR>
if (MyCompareStr(string, "<BR>"))
{
mfCurrentState = knaHasBreak;
}
else mfCurrentState = knaParsing;
break;
case knaHasBreak: //from here, remember <A> marks, abort on <P>, save on </P>
if (MyCompareStr(string, "<A ")&&(mfTheURL[0]==0))
{
if (HTMLExtractor::ParseGoodURL(string+2, mfTheURL))
{
AddEntry(mfTheSubject,mfTheURL);
mfCurrentState = knaParsing;
}
}
else if (MyCompareStr(string, "<\P>")) //OK, we got a success!
{
//Add the entry
if (!mfTheURL[0])
AddEntry(mfTheSubject, "\phttp://www.apple.com");
else AddEntry(mfTheSubject, mfTheURL);
mfCurrentState = knaParsing;
}
else if (MyCompareStr(string, "<P>")) //Something wrong here
{
mfCurrentState = knaParsing;
}
break;
case knaHasLink: //for this, we only want an img, if there's an alt text
if (MyCompareStr(string, "<IMG "))
{
FindATag(string+4, (char*)&mfTheSubject[1], "ALT");
mfTheSubject[0] = strlen( (char*)&mfTheSubject[1] );
if ((mfTheSubject[0]>0)&&(!EqualString(mfTheSubject,"\pApple Sites Worldwide", false, false)))
{
AddEntry(mfTheSubject, mfTheURL);
}
}
else if (MyCompareStr(string, "</A>"))
mfCurrentState = knaParsing;
break;
}
}
else
{
if (mfCurrentState==knaHasStrong) //OK, get got a headline!
{
if (numchars>255)
numchars = 255;
mfTheSubject[0] = numchars;
BlockMove(string, &mfTheSubject[1], numchars); //remember it for later
}
else if ((mfCurrentState!=knaHasBreak)&&(mfCurrentState!=knaHasLink))
mfCurrentState = knaParsing; //and wait for tne next headline
}
}
void LoadAppleCom(sMyDataPtr gGlobalsPtr)
{
AppleExtractor* theparser = new AppleExtractor(gGlobalsPtr);
theparser->ReadEntries();
delete theparser;
InitCursor();
}
// See if we must reload. Check the time to check, then get the last modified time
Boolean MustReloadAppleCom(sMyDataPtr gGlobalsPtr)
{
unsigned long now;
Str31 newModifiedTime;
GetDateTime(&now);
if (now<gAppleNextTime) //time to check yet?
return false;
GetModifiedDate( gGlobalsPtr, kAppleAddress, newModifiedTime);
if (EqualString(newModifiedTime, gAppleLastMod, false, false))
{
gAppleNextTime = now + kAppleNewsPeriod;
return false;
}
else
{
return true;
}
}